/** 
 * Project Name : cloudlead-utils-async 
 * File Name : WithRecoveryAsyncTaskService.java 
 * Package Name : com.cloudlead.utils.async 
 * Date : 2014年7月18日上午9:01:01 
 * Copyright (c) 2014, cloudlead.com.cn All Rights Reserved. 
 * 
 */
package com.cloudlead.utils.async;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 停止服务时没有执行的任务会在重启后继续执行
 * 
 * @Description 带有恢复功能的异步任务服务
 * 
 * @date 2014年7月18日 上午9:01:01
 * @author 陆林(lulin@cloudlead-partners.com)
 * @version 2.0
 * @since 2.0
 */
public class WithRecoveryAsyncTaskService extends SimpleAsyncTaskService {
	/**
	 * 存储任务文件
	 */
	protected File storeTasksFile;

	public WithRecoveryAsyncTaskService() {
		super();
		storeTasksFile = new File(System.getProperty("java.io.tmpdir"),
				"Store_Tasks_");
	}

	/*
	 * @see com.cloudlead.utils.async.DefaultAsyncTaskService#start()
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void start() {
		super.start();

		// 读没处理完成的回调函数处理任务
		if (storeTasksFile.exists()) {
			List<AsyncTask> asynWorkList = new ArrayList<AsyncTask>();
			InputStream input = null;
			ObjectInputStream oi = null;
			try {
				input = new FileInputStream(storeTasksFile);
				oi = new ObjectInputStream(input);
				asynWorkList = (List<AsyncTask>) oi.readObject();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (oi != null) {
					try {
						oi.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (input != null) {
					try {
						input.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

			for (AsyncTask resultInvokeThread : asynWorkList) {
				addTask(resultInvokeThread);
			}
		}
	}

	/*
	 * @see com.cloudlead.utils.async.DefaultAsyncTaskService#stop(long)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void stop(long waitTime) {
		super.stop(waitTime);

		// 写没有处理完成的任务
		OutputStream out = null;
		ObjectOutputStream oo = null;
		// 写没处理完成的回调函数处理任务
		try {
			if (storeTasksFile.exists()) {
				storeTasksFile.delete();
			}
			storeTasksFile.createNewFile();

			out = new FileOutputStream(storeTasksFile);
			oo = new ObjectOutputStream(out);
			List<AsyncTask> list = new ArrayList<AsyncTask>();
			Iterator<Runnable> asynIterator = asyncTaskQueue.iterator();

			while (asynIterator.hasNext()) {
				list.add(((AsyncTaskHandle) asynIterator.next()).task);
			}
			oo.writeObject(list);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (oo != null) {
				try {
					oo.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		logger.debug("In queue has " + asyncTaskQueue.size() + " tasks no run!");

	}

}
